$(function() {
	var ZhangJieGrid = $("#ZhangJieGrid");
	var zhangjieGridAction = $("#zhangjieGridAction");
	
	// 初始化
	ZhangJieGrid.datagrid({
		url : 'zhangjie/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true, // 是否显示分页栏
		fitColumns : true,// 自适应大小
		columns : [ [
				{
					field : 'chapter_name',
					title : '章节名称',
					align : 'center',
					width : 150
				},
				{
					field : 'phase_name',
					title : '所属阶段',
					align : 'center',
					width : 100
				},
				{
					field : 'adding_time',
					title : '章节添加时间',
					sortable : true,
					align : 'center',
					width : 150,
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 150,
					formatter : function(value, row, index) {
						/*		var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr delete">删除</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr queryinfo">查询详情</a>');*/
						return zhangjieGridAction.children("a.ctr").attr('data-id',row.id).attr('data-index', index).end().html();

					}
				} ] ],
		toolbar :"#zhangjieGridToolbar"
	});

	var phaseId = $("#phaseId");
	var chapterName = $("#chapterName");
	$("#zhangjieSearch").on('click', function() {
		ZhangJieGrid.datagrid("load", {
			phaseId : phaseId.val(),
			chapterName : "%" + chapterName.val() + "%"
		});
	});

	var zhangJieForm = $("#zhangJieSearchForm");
	$("#zhangjieReset").on('click', function() {
		zhangJieForm.form("clear");
		ZhangJieGrid.datagrid("load", {})
		// $("#phaseId").find("option[text='-请选择-']").attr("selected",true);
	});

	var gridPanel = ZhangJieGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = ZhangJieGrid.datagrid("getRows");
		var id = rows[index].chapter_id;
		addZhangJieForm(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = ZhangJieGrid.datagrid("getRows");
		var id = rows[index].chapter_id;
		$.messager.confirm("提示", "你确定要删除，删除之后该章节下面的题目就会看不见了哦~", function(r) {
			if (r) {
				$.get("zhangjie/delete/" + id, function(rs) {
					if (rs.success) {
						ZhangJieGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("click", "a.create", function() {
		addZhangJieForm();
	})

	function addZhangJieForm(id) {
		var dialog = $("<div/>")
				.dialog(
						{
							href : 'jiaoxue/jx-add-zhangjie'
									+ (id ? ("/" + id) : ""),
							title : '章节',
							height : '280',
							width : '340',
							modal : true,
							onClose : function() {
								// 窗口关闭的同时销毁此窗口
								$(this).dialog("destroy");
							},
							buttons : [
									{
										iconCls : 'icon-save',
										text : '添加',
										handler : function() {
											// 获取表单中的内容
											var addZhangJieForm = $("#addZhangJieForm");
											if (addZhangJieForm.form('validate')) {
												$
														.post(
																"zhangjie/save",
																addZhangJieForm
																		.serialize(),
																function(rs) {
																	if (rs.success) {
																		ZhangJieGrid
																				.datagrid("reload");
																		dialog
																				.dialog("close");
																	} else {
																		$.messager
																				.alert(
																						"提示",
																						rs.message);
																	}
																});
											}

										}
									}, {
										iconCls : 'icon-clear',
										text : '取消',
										handler : function() {
											dialog.dialog("close");
										}
									} ]
						});
	}
});